package com.blithe.wgt.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blithe.wgt.R;
import com.blithe.wgt.entity.Path;
import com.blithe.wgt.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class PathAdapter extends RecyclerView.Adapter<PathAdapter.HolderView> implements View.OnClickListener{

    private List<Path>  pathList;
    private Context mContext;

    private OnItemClickListener<Path>  onItemClickListener;
    public PathAdapter(List<Path>  pathList, Context context){
        this.pathList=pathList;
        this.mContext=context;
    }
    @NonNull
    @Override
    public HolderView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView =LayoutInflater.from(mContext).inflate(R.layout.item_path, parent, false);
        return new HolderView(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull HolderView holder, int position) {
        Path  item = pathList.get(position);
        Glide.with(mContext).load(item.getPictureURL()).apply(new RequestOptions().override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)).into(holder.img_hero);
        //Logger.e("onBindViewHolder "+position);
        holder.tv_path_name.setText(item.getPathName());
        holder.tv_distance.setText(item.getDistance()+"米");
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return   null == pathList ? 0 : pathList.size();
    }

    public List<Path> getPathEntities() {
        return pathList;
    }

    public void setPathEntities(List<Path> heroEntities) {
        this.pathList = heroEntities;
    }


    public OnItemClickListener<Path> getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener<Path> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onClick(View view) {
        //Toasty.info(mContext,"点击"+ view.getTag()).show();
        if(onItemClickListener!=null){
            onItemClickListener.OnClick(pathList.get((Integer) view.getTag()));
        }
    }

    class HolderView extends RecyclerView.ViewHolder {
        private TextView tv_path_name;
        private TextView tv_distance;
        private ImageView img_hero;
        public HolderView(View itemView) {
            super(itemView);
            tv_path_name = itemView.findViewById(R.id.tv_path_name);
            tv_distance = itemView.findViewById(R.id.tv_distance);

            img_hero= itemView.findViewById(R.id.image_path);
        }
    }
}
